package com.yoomiito.app.ui.anyview.goodsinfo.bottom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.interf.BaseBottomBar;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.ui.order.sureorder.singleGoods.BuyGoodsActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.AllEventID;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.TouchUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;

/**
 * 爆品详情----底部状态栏
 */
public class BaoPinBottomBar extends BaseBottomBar {
    private TextView mShowYouMiTv;
    private TextView mAwardTv;
    private ImageView mAwardIv;

    public BaoPinBottomBar(Context context, boolean isLogin) {
        super(context, isLogin);
        LayoutInflater.from(context).inflate(R.layout.item_bottom_btn_1, this, true);
        initView();
    }

    private void initView() {
        mShowYouMiTv = findViewById(R.id.show_youmi);
        mAwardTv = findViewById(R.id.award);
        mAwardIv = findViewById(R.id.iv_1);
        findViewById(R.id.share).setOnClickListener(v -> {
            if (checkClick()) {
                TouchUtils.onEvent(AllEventID.BaoPinShareEvnetId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent());
                RouterUtils.toShareGoods((BaseActivity) mContext, String.valueOf(mGoodsDetail.getNum_iid()), AllCode.BaoPin);
            }
        });
        findViewById(R.id.add_shop_car).setOnClickListener(v -> {
            if (!mLogin) {
//                AllUtils.toLogin((BaseActivity) mContext, AllCode.LoginIn);
                LoginUtils.INSTANCE.toOneKeyLogin((BaseActivity)mContext);
                return;
            }
            dispatchEvent(false, true, false);
        });
        findViewById(R.id.bg_2).setOnClickListener(v -> {
            if (checkClick()) {
                TouchUtils.onEvent(AllEventID.BaoPinBuyEventId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent());
                BuyGoodsActivity.start(mContext, mGoodsDetail, AllCode.BaoPin);
//                canBuy(String.valueOf(mGoodsDetail.getNum_iid()))
//                        .subscribe(new ApiSubscriber<BaseRepository>() {
//                            @Override
//                            public void onSuccess(BaseRepository baseModel) {
//                            }
//
//                            @Override
//                            public void onFail(ApiException error) {
//                                YMToast.showText(error.getMsg());
//                            }
//                        });
            }
        });
    }


    @Override
    public void updateInfo(GoodsDetail detail) {
        super.updateInfo(detail);
        if (detail.getYo_money() != 0) {
            mShowYouMiTv.setText("送" + detail.getYo_money() + "优米");
            mShowYouMiTv.setVisibility(VISIBLE);
        }
        if (App.mAppConfig.getIsLogin()) {
            if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_4) {//分享返佣
                if (!SpannableUtils.isEmptyOr0(detail.getGet_money())) {
                    mAwardTv.setText("奖励：¥" + SpannableUtils.handlePrice(detail.getGet_money()));
                    mAwardTv.setVisibility(View.VISIBLE);
                    mAwardIv.setVisibility(View.VISIBLE);
                }

            } else {
                mAwardTv.setVisibility(View.GONE);
                mAwardIv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void noShow() {
        super.noShow();
        mAwardTv.setVisibility(View.GONE);
        mAwardIv.setVisibility(View.GONE);
        mShowYouMiTv.setVisibility(GONE);
    }
}
